<!--基础数据页-->
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">印章管理</span>
    <el-button  type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
  <!-- 印章搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="印章名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="seal.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadSealInfo">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0 40px 10px;">
      <i class="bi bi-card-checklist"></i>印章类型
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addSealType" >新增印章</el-button>
    </div>
    <el-table :data="sealInfoArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="名称" align="center"/>
      <el-table-column prop="userName" label="保管人" align="center"/>
      <el-table-column prop="status" label="状态" align="center"
                       :formatter="statusSealFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editSealInfo(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteSealInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 新增印章类型弹窗 -->
  <el-dialog  style="width:1000px;padding:40px;" v-model="dialogVisible" :title="dialogTitle"
              :before-close="handleClose">
    <el-form label-width="80px" label-position="top" >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="名称" >
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="1" v-model="saveSealInfoForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="保管人姓名">
            <el-select placeholder="请选择" v-model="saveSealInfoForm.userID">
              <el-option v-for="item in UserArr" :label="item.username" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="印章状态">
            <el-select placeholder="请选择" v-model="saveSealInfoForm.status" >
              <el-option v-for="item in statusSealArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible=false" >取消</el-button>
      <el-button type="primary" @click="savaSealInfo">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>

import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//控制新增弹窗是否显示
const dialogVisible = ref(false);
//定义弹窗
const dialogTitle = ref('新增');
//1.定义数组存放印章表单数据
const sealInfoArr = ref([]);
//定义搜索关键字
const seal = ref({
  name: ''
});
//定义处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭弹窗吗?')){
    saveSealInfoForm.value = {};
    dialogVisible.value = false;
  }
}
//定义加载方法
const loadSealInfo =()=>{
  let data = qs.stringify(seal.value)
  axios.get(BASE_URL+'/oa1/seal/select?'+data).then((response)=>{
    if(response.data.code === 2000){
      sealInfoArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  loadSealInfo()
})
//定义新增审批类型
const addSealType = ()=>{
  dialogVisible.value = true;
}
//定义重置搜索
const resetSearch = ()=>{
  seal.value = {};
  loadSealInfo();
}

//对印章状态定义
const statusSealArr = ref([]);
const statusSealFormatter = (row,column,cellValue)=>{
  if(!statusSealArr.value){
    return cellValue;
  }
  for(let item of statusSealArr.value){
    if(item.value === cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

onMounted(()=>{
  loadDictOption(statusSealArr,'seal_status');
  loadUser();
})

//定义一个对象保存表单数据
const saveSealInfoForm =ref({
  userID:'',
  name:'',
  custodianId:'',
  status:'',
})
const savaSealInfo =()=>{
  dialogTitle.value='新增审批类型';
  let data = qs.stringify(saveSealInfoForm.value)
  axios.post(BASE_URL+'/oa1/seal/save',data).then((response)=>{
    if(response.data.code === 2000){
      ElMessage.success('保存成功!');
      //保存成功后,需要清空表单数据,关掉弹窗
      saveSealInfoForm.value = {};
      dialogVisible.value = false;
      loadSealInfo();
    }else{
      //数据库里的code和license设置了唯一约束,如果插入重复的值会操作失败,我们可以设置明确的提示语句
      ElMessage.error(response.data.msg);
    }
  })
}
const editSealInfo = (id)=>{
  console.log(id)
  dialogVisible.value = true;
  dialogTitle.value='修改审批类型';
  axios.get(BASE_URL+'/oa1/seal/select?id='+id).then((response)=>{
    if(response.data.code === 2000){
      saveSealInfoForm.value = response.data.data[0];
      dialogVisible.value = true;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//删除
const deleteSealInfo = (id)=>{
  if(confirm('您确认要删除吗?')){
    axios.post(BASE_URL+'/oa1/seal/delete/'+id).then((response)=>{
      if(response.data.code === 2000){
        ElMessage.success('删除成功!');
        loadSealInfo();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
const searchUserForm = ref({status: '', name: ''});
const UserArr = ref([]);
const loadUser = () => {
  let data = qs.stringify(searchUserForm.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL + '/oa1/user/select?' + data)
      .then((response) => {//回调函数中处理响应结果
        if (response.data.code == 2000) {
          UserArr.value = response.data.data;//将后端响应数据装入用户数组中
          console.log(UserArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

</script>

<style scoped>

</style>